package com.wujilee.wujileenetty.demos.web.netty.heima;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoop;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.util.concurrent.DefaultEventExecutorGroup;
import io.netty.util.concurrent.DefaultPromise;
import lombok.extern.slf4j.Slf4j;

import java.nio.charset.Charset;
import java.util.concurrent.ExecutionException;

/**
 * @author liwuji
 * @date 2024/8/7
 * @desc
 **/
@Slf4j
public class EventLoopService {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
//        new ServerBootstrap()
//                .group(new NioEventLoopGroup())
//                .channel(NioServerSocketChannel.class)
//                .childHandler(new ChannelInitializer<NioSocketChannel>() {
//                    @Override
//                    protected void initChannel(NioSocketChannel ch) throws Exception {
////                        ch.pipeline().addLast(new StringDecoder());
//                        log.info("服务端接收到了新的连接");
//                        ch.pipeline().addLast(new ChannelInboundHandlerAdapter() {
//                                                  @Override
//                                                  public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
//                                                      ByteBuf buffer = (ByteBuf) msg;
//                                                      log.debug("服务端接收到数据:{}", buffer.toString(Charset.defaultCharset()));
//                                                      // 下面方法的执行需要主动调用该方法
//                                                      ctx.fireChannelRead(msg);
//                                                  }
//                                              }
//
//                        ).addLast(new DefaultEventExecutorGroup(1), "handle1", new ChannelInboundHandlerAdapter() {
//                            // 该方法的执行是异步的
//                            @Override
//                                    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
//                                        ByteBuf buffer = (ByteBuf) msg;
//                                        log.debug("服务端接收到数据:{}", buffer.toString(Charset.defaultCharset()));
//                                        Thread.sleep(1000L);
//                                    }
//                                }
//                        );
//
//                    }
//                })
//                .bind(6666);
//    }
        NioEventLoopGroup eventExecutors = new NioEventLoopGroup();

        EventLoop next = eventExecutors.next();
        DefaultPromise<Integer> promise = new DefaultPromise<>(next);
        new Thread(()->{
            try {
                Thread.sleep(1000L);
                promise.setSuccess(100);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }).start();

        new Thread(()->{
            try {
                Thread.sleep(2000L);
                promise.setSuccess(200);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }).start();

        System.out.println("等待异步结果");
        System.out.println(promise.get());

    }
}
